package com.by;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ServivE
 * @version 0.1
 * @className Productor
 * @description
 * @date 2024/10/21 9:11
 * @since jdk17
 */
public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂  创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("erp");//虚拟主机  默认是 /
        factory.setPort(5672);//连接端口号
        factory.setHost("192.168.203.130");//服务器地址 默认是localhost
        factory.setUsername("admin");
        factory.setPassword("123456");

        //2创建连接  资源资源自动释放(连接 信道 关闭)
        try (Connection conn = factory.newConnection(); Channel channel = conn.createChannel()) {
            //3.声明交换器
            String exName = "pubsub";
            channel.exchangeDeclare(exName, BuiltinExchangeType.FANOUT);

            //4.声明队列
            String q1 = "queueOne";
            channel.queueDeclare(q1, false, false, false, null);

            String q2 = "queueTwo";
            channel.queueDeclare(q2, false, false, false, null);

            //5.绑定队列与交换器
            channel.queueBind(q1, exName, "");
            channel.queueBind(q2, exName, "");
            //6.发送消息
            channel.basicPublish(exName, "", null, "Hello".getBytes());

        }

    }
}
